// import indoorNavigationData from './data/indoor_navigation_data.json';
import {space_face} from './data/space_face';
import {paranoma} from './data/paranoma_data';

const floorParkColor = "rgb(233,244,255)";
const roadColor = "rgb(47,130,214)";
const availParkColor = "rgb(68,159,235)";
const inUseParkColor = "#e65e97";
const waitingParkColor = 'rgb(173,195,192)';
const buildingParkColor = "rgb(209,233,255)";
const navigationPathColor = 'rgb(25,148,117)';
const lineWidthProp = {"stops": [[17, 1], [20, 5.5]], "base": 1};
const iconSizeProp = {"stops": [[17, 0.1], [20, 1]], "base": 1};
const textSizeProp = {"stops": [[17, 4], [20, 10]], "base": 1};


const sourceNames = ['innerDrivingRoute','paranoma-source1', 'line-source1', 'road-source1', 'park-source1', 'poi-source1', 'line-source2', 'road-source2', 'park-source2'];


class InnerParkingMapUtil {

    showFloor(floor, map) {
        if (floor === 'F1') {
            map.setLayoutProperty('space_lines1', 'visibility', 'visible');
            map.setLayoutProperty('space_road1', 'visibility', 'visible');
            map.setLayoutProperty('park-layer-stop1', 'visibility', 'visible');
            map.setLayoutProperty('park-layer-spot1', 'visibility', 'visible');
            map.setLayoutProperty('park-layer-floor1', 'visibility', 'visible');
            map.setLayoutProperty('poi1', 'visibility', 'visible');
            map.setLayoutProperty('paranoma1', 'visibility', 'visible');

            map.setLayoutProperty('space_lines2', 'visibility', 'none');
            map.setLayoutProperty('space_road2', 'visibility', 'none');
            map.setLayoutProperty('park-layer-stop2', 'visibility', 'none');
            map.setLayoutProperty('park-layer-spot2', 'visibility', 'none');
            map.setLayoutProperty('park-layer-floor2', 'visibility', 'none');
        } else {
            map.setLayoutProperty('space_lines1', 'visibility', 'none');
            map.setLayoutProperty('space_road1', 'visibility', 'none');
            map.setLayoutProperty('park-layer-stop1', 'visibility', 'none');
            map.setLayoutProperty('park-layer-spot1', 'visibility', 'none');
            map.setLayoutProperty('park-layer-floor1', 'visibility', 'none');
            map.setLayoutProperty('poi1', 'visibility', 'none');
            map.setLayoutProperty('paranoma1', 'visibility', 'none');

            map.setLayoutProperty('space_lines2', 'visibility', 'visible');
            map.setLayoutProperty('space_road2', 'visibility', 'visible');
            map.setLayoutProperty('park-layer-stop2', 'visibility', 'visible');
            map.setLayoutProperty('park-layer-spot2', 'visibility', 'visible');
            map.setLayoutProperty('park-layer-floor2', 'visibility', 'visible');
        }
    }

    //显示内部导航线
    showInnerDrivingRoute(map, data) {
        map.getSource('innerDrivingRoute').setData({
            "type": "Feature",
            "properties": {},
            "geometry": {
                "type": "LineString",
                "coordinates": data
            }
        })
    }

    addInnerParkingLayers(map) {
        if (map.getSource('line-source1')) {
            return;
        }
        map.addSource("innerDrivingRoute", {
            "type": "geojson",
            "data": {
                "type": "Feature",
                "properties": {},
                "geometry": {
                    "type": "LineString",
                    "coordinates": [
                        [0, 0],
                        [0, 0]
                    ]
                }
            }
        });

        map.addLayer({
            "id": "innerDriving",
            "type": "line",
            "source": "innerDrivingRoute",
            "layout": {
                "line-join": "round",
                "line-cap": "round"
            },
            "paint": {
                "line-color": "#00ff00",
                "line-width": 6,
                'render3d': true,
                "line-height": 0.28
            }
        });

        //添加第一层线
        map.addSource('line-source1', {
            type: 'geojson',
            data: './data/space_face_line.json'
        });
        map.addLayer({
            "id": "space_lines1",
            "type": "line",
            "source": "line-source1",
            "layout": {
                "line-join": "round",
                "line-cap": "round"
            },
            "paint": {
                "line-color": roadColor,
                "line-width": 0.5,
                'render3d': true,
                "line-height": 0.22
            }
        });

        //添加第一层road
        map.addSource('road-source1', {
            type: 'geojson',
            data: './data/road.json'
        });
        map.addLayer({
            "id": "space_road1",
            "type": "line",
            "source": "road-source1",
            "layout": {
                "line-join": "round",
                "line-cap": "round"
            },
            "paint": {
                "line-color": roadColor,
                "line-width": lineWidthProp,
                'render3d': true,
                "line-height": 0.22
            }
        });

        //第一层不可同行区域
        map.addSource('park-source1', {
            type: 'geojson',
            data: space_face
        });
        map.addLayer({
            id: 'park-layer-stop1',
            source: 'park-source1',
            type: 'extrusion',
            paint: {
                'extrusion-height': 3,
                'extrusion-color': buildingParkColor,
                'extrusion-opacity': 0.8
            },
            filter: ["==", "space_type", 0]
        });
        //第一层车位
        map.addLayer({
            id: 'park-layer-spot1',
            source: 'park-source1',
            type: 'extrusion',
            paint: {
                'extrusion-height': 0.2,
                'extrusion-color': {

                    "property": "status",
                    "stops": [
                        [0, availParkColor],
                        [1, inUseParkColor],
                        [2, waitingParkColor]
                    ]
                },
                'extrusion-opacity': 0.8
            },
            filter: ['==', 'space_type', 1]
        });
        //第一层地板
        map.addLayer({
            id: 'park-layer-floor1',
            source: 'park-source1',
            type: 'extrusion',
            paint: {
                'extrusion-height': 0.1,
                'extrusion-color': floorParkColor,
                'extrusion-opacity': 0.8
            },
            filter: ['==', 'space_type', -1]
        });

        //第一层poi
        map.addSource('poi-source1', {
            type: 'geojson',
            data: './data/poi.json'
        });
        map.addLayer({
            "id": "poi1",
            "type": "symbol",
            "source": "poi-source1",
            "layout": {
                "icon-image": "{icon}",
                "text-field": "{poi_name}",
                "text-offset": [0, 1.6],
                "text-anchor": "top",
                "icon-allow-overlap": true,  //图标允许压盖
                "text-allow-overlap": true,   //图标覆盖文字允许压盖
                "icon-size": iconSizeProp,
                "text-size": textSizeProp
            },
            "paint": {
                "render3d": true,
                "symbol-height": 1.0
            }
        });

        //第一层全景点位
        map.addSource('paranoma-source1', {
            type: 'geojson',
            data: paranoma
        });
        map.addLayer({
            "id": "paranoma1",
            "type": "symbol",
            "source": "paranoma-source1",
            "layout": {
                "icon-image": "{icon}",
                "text-field": "{title}",
                "text-offset": [0, 1.6],
                "text-anchor": "top",
                "icon-allow-overlap": true,  //图标允许压盖
                "text-allow-overlap": true,   //图标覆盖文字允许压盖
                "icon-size": iconSizeProp,
                "text-size": textSizeProp
            },
            "paint": {
                "render3d": true,
                "symbol-height": 1.0
            }
        });


        //添加第二层

        //添加第二层线
        map.addSource('line-source2', {
            type: 'geojson',
            data: './data/space_face_line_b2.json'
        });

        map.addLayer({
            "id": "space_lines2",
            "type": "line",
            "source": "line-source2",
            "layout": {
                "line-join": "round",
                "line-cap": "round",
                "visibility": 'none'
            },
            "paint": {
                "line-color": roadColor,
                "line-width": 0.5,
                'render3d': true,
                "line-height": 30.22
            }
        });

        // 添加第二层road
        map.addSource('road-source2', {
            type: 'geojson',
            data: './data/road_b2.json'
        });

        map.addLayer({
            "id": "space_road2",
            "type": "line",
            "source": "road-source2",
            "layout": {
                "line-join": "round",
                "line-cap": "round",
                "visibility": 'none'
            },
            "paint": {
                "line-color": roadColor,
                "line-width": lineWidthProp,
                'render3d': true,
                "line-height": 30.22
            }
        });

        map.addSource('park-source2', {
            type: 'geojson',
            data: './data/space_face_b2.json'
        });
        //第二层不可通行区域
        map.addLayer({
            id: 'park-layer-stop2',
            source: 'park-source2',
            type: 'extrusion',
            "layout": {
                "visibility": 'none'
            },
            paint: {
                'extrusion-height': 33,
                'extrusion-base': 30,
                'extrusion-color': buildingParkColor,
                'extrusion-opacity': 0.7
            },
            filter: ["==", "space_type", 0]
        });
        //第二层停车区域
        map.addLayer({
            id: 'park-layer-spot2',
            source: 'park-source2',
            type: 'extrusion',
            "layout": {
                "visibility": 'none'
            },
            paint: {
                'extrusion-height': 30.2,
                'extrusion-base': 30,
                'extrusion-color': {

                    "property": "status",
                    "stops": [
                        [0, availParkColor],
                        [1, inUseParkColor],
                        [2, waitingParkColor]
                    ]
                },
                'extrusion-opacity': 0.7
            },
            filter: ['==', 'space_type', 1]
        });
        //第二层地板
        map.addLayer({
            id: 'park-layer-floor2',
            source: 'park-source2',
            type: 'extrusion',
            "layout": {
                "visibility": 'none'
            },
            paint: {
                'extrusion-height': 30.1,
                'extrusion-base': 30,
                'extrusion-color': floorParkColor,
                'extrusion-opacity': 0.7
            },
            filter: ['==', 'space_type', -1]
        });
    }

    //TODO 删除所有室内停车场图层的逻辑代码
    removeInnerParkingLayers(map) {
        map.removeLayer('space_lines1');
        map.removeLayer('space_road1');
        map.removeLayer('park-layer-stop1');
        map.removeLayer('park-layer-spot1');
        map.removeLayer('park-layer-floor1');
        map.removeLayer('poi1');
        map.removeLayer('paranoma1');

        map.removeLayer('space_lines2');
        map.removeLayer('space_road2');
        map.removeLayer('park-layer-stop2');
        map.removeLayer('park-layer-spot2');
        map.removeLayer('park-layer-floor2');

        for (let i = 0; i < sourceNames.length; i++) {
            map.removeSource(sourceNames[i]);
        }


    }

    // 显示停车场图层
    showInnerParkingLayers(map) {
        map.setLayoutProperty('space_lines1', 'visibility', 'visible');
        map.setLayoutProperty('space_road2', 'visibility', 'visible');
        map.setLayoutProperty('park-layer-stop1', 'visibility', 'visible');
        map.setLayoutProperty('park-layer-spot1', 'visibility', 'visible');
        map.setLayoutProperty('park-layer-floor1', 'visibility', 'visible');
        map.setLayoutProperty('poi1', 'visibility', 'visible');
        map.setLayoutProperty('paranoma1', 'visibility', 'visible');

        map.setLayoutProperty('space_lines2', 'visibility', 'visible');
        map.setLayoutProperty('space_road2', 'visibility', 'visible');
        map.setLayoutProperty('park-layer-stop2', 'visibility', 'visible');
        map.setLayoutProperty('park-layer-spot2', 'visibility', 'visible');
        map.setLayoutProperty('park-layer-floor2', 'visibility', 'visible');
    }

    // 隐藏停车场图层
    hideInnerParkingLayers(map) {
        map.setLayoutProperty('space_lines1', 'visibility', 'none');
        map.setLayoutProperty('space_road2', 'visibility', 'none');
        map.setLayoutProperty('park-layer-stop1', 'visibility', 'none');
        map.setLayoutProperty('park-layer-spot1', 'visibility', 'none');
        map.setLayoutProperty('park-layer-floor1', 'visibility', 'none');
        map.setLayoutProperty('poi1', 'visibility', 'none');
        map.setLayoutProperty('paranoma1', 'visibility', 'none');

        map.setLayoutProperty('space_lines2', 'visibility', 'none');
        map.setLayoutProperty('space_road2', 'visibility', 'none');
        map.setLayoutProperty('park-layer-stop2', 'visibility', 'none');
        map.setLayoutProperty('park-layer-spot2', 'visibility', 'none');
        map.setLayoutProperty('park-layer-floor2', 'visibility', 'none');
    }

    // 添加导航路线及高亮车位
    addInnerNavigation(map) {
        map.addSource('indoor-navigation-path', {
            type: 'geojson',
            data: {
                "type": "geojson",
                "data": {
                    "type": "FeatureCollection",
                    "features": []
                }
            }
        });
        map.addLayer({
            "id": "ndoor-navigation-path",
            "type": "line",
            "source": "ndoor-navigation-path",
            "layout": {
                "line-join": "round",
                "line-cap": "round"
            },
            "paint": {
                "line-color": navigationPathColor,
                "line-width": lineWidthProp,
                'render3d': true,
                "line-height": 0.22
            }
        });

        map.addLayer({
            id: 'park-layer-spot-height-light',
            source: 'park-source1',
            type: 'extrusion',
            paint: {
                'extrusion-height': 0.2,
                'extrusion-color': 'rgb(233,240,29)',
                'extrusion-opacity': 0.8
            },
            filter: ['==', 'space_id', -1]
        });
    }

    //控制已选车位状态
    setSelectedSpotStatus(map, id, status) {
        let newSpaceFace = space_face;
        for (let i = 0; i < newSpaceFace.features.length; i++) {
            let aFeat = newSpaceFace.features[i];
            if (id === aFeat.properties.space_id) {
                aFeat.properties.status = status;

                map.getSource('park-source1').setData(newSpaceFace);

                return;
            }
        }
    }
}

export default InnerParkingMapUtil;